﻿using LDAP.BLL;
using LDAP.Entity;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace LDAP.Web.View.Attendance
{
    public partial class CalendarMgmt : AngelBasePage
    {
        public int Year;
        public int Month;

        protected void Page_Load(object sender, EventArgs e)
        {
            DateTime defaultTime = new DateTime(DateTime.Today.AddMonths(1).Year, DateTime.Today.AddMonths(1).Month, 1);

            Year = Tools.Web.Request.GetQueryInt("y", 0);
            Month = Tools.Web.Request.GetQueryInt("m", 0);

            try
            {
                DateTime t = new DateTime(Year, Month, 1);
                if (t <= defaultTime)
                    t = defaultTime;

                Year = t.Year;
                Month = t.Month;
            }
            catch
            {
                Year = defaultTime.Year;
                Month = defaultTime.Month;
            }

            Calender.Text = GetCalendar(Year, Month);
        }


        private static string GetCalendar(int year, int month)
        {
            StringBuilder sb = new StringBuilder();

            List<int> days = new List<int>();

            DateTime begin = new DateTime(year, month, 1);

            for (int i = 0; i < (int)begin.DayOfWeek; i++)
                days.Add(0);

            for (int i = 1; i <= (begin.AddMonths(1) - begin).Days; i++)
                days.Add(i);

            int count = days.Count % 7;
            if (count != 0)
            {
                for (int i = 0; i < 7 - count; i++)
                    days.Add(0);
            }

            MonthSchedule ms = BizMonthSchedule.GetByDate(begin);

            bool start = false;
            for (int i = 0; i < days.Count; i++)
            {
                if (start && i % 7 == 0)
                {
                    sb.Append("</tr>");
                }
                if (i % 7 == 0)
                {
                    start = true;
                    sb.Append("<tr>");
                }
                
                string dayofweek = "";
                if (days[i] > 0)
                {
                    if (ms != null) {
                        if (ms.Weekend.IndexOf("," + days[i] + ",") > -1)
                            dayofweek = "weekend";
                    }
                    else
                    {
                        DateTime dt = new DateTime(year, month, days[i]);
                        if (dt.DayOfWeek == DayOfWeek.Sunday || dt.DayOfWeek == DayOfWeek.Saturday)
                            dayofweek = "weekend";
                    }
                }

                sb.Append("<td class='" + dayofweek + "'>" + (days[i] > 0 ? days[i].ToString() : "") + "</td>");
                if (start && i % 7 == 0)
                {
                    start = false;
                }
            }

            if (days.Count % 7 != 0)
                sb.Append("</tr>");

            return sb.ToString();
        }
    }
}